package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabDiagnosTemp;
import com.ysd.lis.entity.lab.LabDiagnosTempDesc;
import com.ysd.lis.mapper.lab.LabDiagnosTempDescMapper;
import com.ysd.lis.mapper.lab.LabDiagnosTempMapper;
import com.ysd.lis.request.lab.DiagnosTempDto;
import com.ysd.lis.service.lab.LabDiagnosTempService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 通道号管理表 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-10-29
 */
@Service
public class LabDiagnosTempServiceImpl extends ServiceImpl<LabDiagnosTempMapper, LabDiagnosTemp> implements LabDiagnosTempService {
    @Autowired
    LabDiagnosTempMapper labDiagnosTempMapper;
    @Autowired
    LabDiagnosTempDescMapper labDiagnosTempDescMapper;
    @Override
    public Result findDiagnosTempList(DiagnosTempDto param) {
        MPJLambdaWrapper<LabDiagnosTemp> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabDiagnosTemp.class);
        lambdaQueryWrapper.eq(LabDiagnosTemp::getDelFlag, 0);

        if(ToolsUtils.isNotEmpty(param.getDiagnosTypeId())){
            lambdaQueryWrapper.eq(LabDiagnosTemp::getDiagnosTypeId,param.getDiagnosTypeId());
        }
        if (StringUtils.isNotBlank(param.getSearchValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(LabDiagnosTemp::getTempCode, param.getSearchValue()).or().like(LabDiagnosTemp::getTempName, param.getSearchValue()));
        }
        List<LabDiagnosTemp> list = labDiagnosTempMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", list);
    }

    @Override
    public Result addDiagnosTemp(LabDiagnosTemp standard) {
        int insert = labDiagnosTempMapper.insert(standard);
        if(insert>0){
            return Result.succ(1, "添加成功", standard);
        }else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editDiagnosTemp(LabDiagnosTemp standard) {
        int updateById = labDiagnosTempMapper.updateById(standard);
        if(updateById>0){
            return Result.succ(1, "修改成功", standard);
        }else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result batchDeleteDiagnosTemp(DiagnosTempDto dto) {
        List<LabDiagnosTemp> dtoDiagnosTempList = dto.getDiagnosTempList();
        List<String> ids = dtoDiagnosTempList.stream().map(LabDiagnosTemp::getId).collect(Collectors.toList());
        //查询标准下是否有项目
        LambdaQueryWrapper<LabDiagnosTempDesc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabDiagnosTempDesc::getDelFlag,0)
                .in(LabDiagnosTempDesc::getDiagnosTempId,ids);
        Integer cnt = labDiagnosTempDescMapper.selectCount(queryWrapper);
        if(cnt>0){
            return Result.succ(1, "标准下有项目，不能删除", null);
        }

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
